# -*- coding: utf-8 -*-
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Users/macmini/Downloads/xiaocode/python_ufile/python_ufile'])
import time

from lib.ucloud.ufile import getufilelist
from lib.ucloud.ufile import deleteufile
from src.ucloud.ufile_config import read_config
# from src.db.ucloud_db import insert_item
from twisted.internet import reactor
from src.sshconn.ssh_conn import singleton
from multiprocessing.dummy import Pool as ThreadPool
from lib.ucloud.logger import logger
# from apscheduler.schedulers.background import BackgroundScheduler
# import asyncio


@singleton
class AutoReadLogList(object):

    def __init__(self):
        self.failureTime = 2592000  # 30天
        public_key, private_key, bucket = read_config()

        self.bucket = bucket
        # 构造对象，并设置公私钥
        self.handler = getufilelist.GetFileList(public_key, private_key)
        self.delete_handler = deleteufile.DeleteUFile(public_key, private_key)

        self.limit = 500
        self.pool = ThreadPool(500)  # 100是线程池中线程的个数
        self.delete_pool = ThreadPool(500)  # 100是线程池中线程的个数
        self.prefix = sys.argv[1]

    def get_next_page(self):
        ret_one, resp = self.handler.getfilelist(self.bucket, prefix=self.prefix, limit=self.limit)
        self.pool.map(self.insert_page_item, ret_one['DataSet'])

        while True:
            if ret_one['NextMarker'] != "":
                # ret, resp = self.handler.getfilelist(self.bucket, limit=self.limit, marker=ret_one['NextMarker'])
                ret, resp = self.handler.getfilelist(self.bucket, prefix=self.prefix, marker=ret_one['NextMarker'], limit=self.limit)
                ret_one = ret
                self.pool.map(self.insert_page_item, ret_one['DataSet'])
            else:
                break
        logger.info("end")
        reactor.stop()

    def insert_page_item(self, item):
        if (time.time() - item.get("CreateTime", 0)) >= self.failureTime:
            # 过期
            self.delete_pool.map(self.delete_item, [item])

    def delete_item(self, item):
        # 删除公共空间的文件
        ret, resp = self.delete_handler.deletefile(self.bucket, item.get('FileName', ''))
        if resp.status_code == 204:
            # 删除成功 修改库记录
            logger.info("delete succ {file}".format(file=item.get('FileName', '')))
        else:
            # 删除失败 修改库记录
            logger.info("delete fail {file}".format(file=item.get('FileName', '')))


if __name__ == "__main__":
    reactor.callWhenRunning(AutoReadLogList().get_next_page)
    reactor.run()
    # scheduler = BackgroundScheduler()
    # # 间隔一天执行一次
    # scheduler.add_job(AutoReadLogList().get_next_page, 'interval', seconds=60*60*24)
    # # 这里的调度任务是独立的一个线程
    # scheduler.start()
